/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.impl;

import cz.muni.fi.pa165.creatures.dao.RegionDAO;
import cz.muni.fi.pa165.creatures.model.Region;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.Region} instances.
 * 
 * @author hulek
 */
@Transactional
@Repository
public class RegionDAOImpl implements RegionDAO {
    
    @PersistenceContext
    private EntityManager em;
   
    private static final Logger logger = 
            Logger.getLogger(RegionDAOImpl.class.getName());
    
    @Override
    public void create(Region region) {
        try {
            DAOUtils.notNull(region);
            em.persist(region);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to create a region. "
                    + "Region cannot be a null object.");
        } catch (EntityExistsException ex) {
            logger.log(Level.INFO, "Unable to create a region. "
                    + "Region to create is already created.");
        }
    }

    @Override
    public void remove(Region region) {
        try {
            DAOUtils.notNull(region);
            Region toRemove = em.find(Region.class, region.getId());
            em.remove(toRemove);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to remove a region. "
                    + "Region cannot be a null object.");
        }
    }
    
    @Override
    public void update(Region region) {
        try {
            DAOUtils.notNull(region);
            em.merge(region);
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to update a region. "
                    + "Region cannot be a null object.");
        }
    }

    @Override
    public Region get(Long id) {
        try {
            DAOUtils.notNull(id);
            Region region = em.find(Region.class, id);
            return region;
        } catch(IllegalArgumentException ex) {
            logger.log(Level.INFO, "Unable to get a region. "
                    + "Id of the region cannot be a null object.");
            return null;
        }
    }

    @Override
    public int getCount() {
        Query q = em.createQuery("select count(o) from Region as o");
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public List<Region> getAll() {
        Query q = em.createQuery("from Region");
        return q.getResultList();
    }    
}
